<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 一级分类 -->
        <XtxBreadItem to="/">{{ goodDetail.categories[1].name }}</XtxBreadItem>
        <!-- 二级分类 -->
        <XtxBreadItem to="/">{{ goodDetail.categories[0].name }}</XtxBreadItem>
        <!-- 商品名 -->
        <XtxBreadItem to="/">{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区=》商品主图 -->
        <div class="media">
          <GoodImg :mainImg="goodDetail.mainPictures" />
          <GoodsSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodInfos />
          <!-- 商品sku信息 -->
          <GoodsSku :goods="goodDetail" @change="getSku" />
          <!-- 商品数量加减 -->
          <XtxNumbox v-model="buyNum" v-model:test="test" :max="goodDetail.inventory" />
          <!-- 添加到购物车按钮 -->
          <XtxButton @click="addCart" style="margin-top: 20px" size="large" type="primary">
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img
            v-for = "(img, i) in goodDetail.details.pictures"
            v-imglazy = 'img'
            :key="i"
             />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, provide } from 'vue'
import { findGoodDetail } from '@/api/good'
import { useRoute } from 'vue-router'
import GoodImg from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodInfos from './components/goods-name.vue'
import msg from '@/components/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodImg,
    GoodsSales,
    GoodInfos
  },
  setup () {
    const goodDetail = ref({})
    // 获取路由参数对象 => route === this.$route
    provide('goodDetail', goodDetail)
    const route = useRoute()
    console.log('v3获取动态路由参数：', route)
    const getGoodDetail = async () => {
      const res = await findGoodDetail(route.params.id)
      console.log(res)
      goodDetail.value = res
    }
    getGoodDetail()
    //  获取商品的sku信息
    const buyNum = ref(1)
    const test = ref(1)
    // 当前选中有效商品sku信息
    const currSel = ref(null)
    const getSku = (currSku) => {
      console.log('当前选择的商品sku信息:', currSku)
      if (currSku.skuId) {
        // 不是空对象，就是一条有效的sku信息=》更新价格、库存信息
        goodDetail.value.price = currSku.price
        goodDetail.value.oldPrice = currSku.oldPrice
        goodDetail.value.inventory = currSku.inventory
        // 存储选择
        currSel.value = currSku
      } else {
        // 清空上次选择
        currSel.value = null
      }
    }
    // 加入购物车业务实现
    const store = useStore()
    const addCart = async () => {
      /**
       * 1. 当前已经选择了有效sku商品信息
       * 2. 有效sku商品信息的库存大于0
       * 满足以上两个条件，才能加入购物车
       */
      if (!currSel.value) {
        return msg({ text: '请选择商品规格！' })
      }
      if (currSel.value.inventory === 0) {
        return msg({ text: '商品卖完了！' })
      }
      // 执行加入购物车: 1. 准备后台需要的加入购物车商品对象 2. 调用vuex的action加入购物车（今天不调用后台接口）
      const addCood = {
        id: goodDetail.value.id,
        name: goodDetail.value.name,
        picture: goodDetail.value.mainPictures[0],
        skuId: currSel.value.skuId,
        price: currSel.value.oldPrice,
        nowPrice: currSel.value.price,
        attrsText: currSel.value.specsText,
        stock: currSel.value.inventory,
        selected: true,
        isEffective: true, // 是否是有效商品
        count: buyNum.value // 加入数量
      }
      console.log(addCood)
      // 调用vuex的action加入购物车
      try {
        const rmsg = await store.dispatch('cart/addCartAction', addCood)
        //  加入成功提示
        msg({ text: rmsg, type: 'success' })
      } catch (error) {
        console.log(error)
      }
    }

    return { goodDetail, getSku, buyNum, test, addCart }
  },
  created () {
    console.log(this.$route.params)
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
